import { IUILogicNodeProvider, exportData } from '@ibiz-template/runtime';
import { IDEUIPFPluginLogic } from '@ibiz/model-core';

export class DataExportProvider implements IUILogicNodeProvider {
  async exec(model: IDEUIPFPluginLogic, ctx: IData): Promise<unknown> {
    const { params } = ctx;
    if (params && params.exportdata && params.exportdata.length > 0) {
      const header = ['人员', '证件号', '电话', '地点', '摘要'];
      const headerId = ['per', 'idn', 'tel', 'loc', 'summary'];
      const formatArrayData = (
        filterVal: string[],
        tableData: IData[],
      ): IData[][] => {
        return tableData.map(v => {
          return filterVal.map(j => {
            return v[j];
          });
        });
      };
      const data = formatArrayData(headerId, params.exportdata);
      return exportData(header, data, model.name!);
    }
    return Promise.resolve();
  }
}
